﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class SyncDeleteRecord : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "SyncDeleteRecord";
            }
        }

      
        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }


        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Sync các bản ghi đã xóa lên máy chủ web");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override Portal.Interface.WebPartInt[] SubWebParts
        {
            get
            {
                Portal.Interface.WebPartInt[] SubWebParts = new Portal.Interface.WebPartInt[]{
                    
                };
                return SubWebParts;
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CMS.SyncDeleteRecord));
                Environments.CheckWebPartAllow(WebPartId, true);
                Environments.CheckSessionTimeOut(true);

                string RetSyncDeleteRecord =
                    Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "SyncDeleteRecord.js") +
                    "<div class=cssSyncDeleteRecordMain>" +
                    "   <div id=divSyncDeleteRecordContent>" + GetSyncDeleteRecord() + "</div>" +
                    "</div>";
                return RetSyncDeleteRecord;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

      
        [AjaxPro.AjaxMethod]
        public string GetSyncDeleteRecord()
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                DataTable dtSyncDeleteRecord = SyncDeleteRecordBll.Reading();
                string RetSyncDeleteRecord=
                    "   <input type=button onclick=javascript:ClientReading(); value=\"" + ResourceBLL.GetText(WebPartId, "ReadingText", "Đọc dữ liệu") + "\"><input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Sync", "Đồng bộ máy chủ web") + "\" onclick=javascript:ClientSyncDeleteRecord();>" +
                    "   <div id=divProcessing></div>";

                if (dtSyncDeleteRecord.Rows.Count == 0)
                {
                    RetSyncDeleteRecord += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetSyncDeleteRecord += 
                               "<div class=cssHeading>"+GetText("TotalDeletedRecord","Tổng số có ")+" ["+dtSyncDeleteRecord.Rows.Count+"] "+GetText("DeletedRecordText","bản ghi đã được xóa")+"</div>"+
                               "<table class=cssSyncDeleteRecordContent>" +
                               "  <tr>" +
                               "      <th class=cssCol0>#</th>" +
                               "      <th class=cssCol0>" + ResourceBLL.GetText(WebPartId, "Object", "Đối tượng") + "</th>" +
                               "      <th class=cssCol1>" + ResourceBLL.GetText(WebPartId, "DeletedDate", "Ngày xóa") + "</th>" +
                               "      <th class=cssCol2>" + ResourceBLL.GetText(WebPartId, "RecordTitle", "Nội dung") + "</th>" +
                               "      <th class=cssCol3>" + ResourceBLL.GetText(WebPartId, "DeletedBy", "Người xóa") + "</th>" +
                               " </tr>";
                    int StartIndex = 0;
                    int EndIndex = dtSyncDeleteRecord.Rows.Count;
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        //string Object = (string)dtSyncDeleteRecord.Rows[iIndex]["Object"];
                        string ObjectName = (string)dtSyncDeleteRecord.Rows[iIndex]["ObjectName"];
                        string RecordId = (string)dtSyncDeleteRecord.Rows[iIndex]["RecordId"];
                        DateTime DeletedDate = (DateTime)dtSyncDeleteRecord.Rows[iIndex]["DeletedDate"];
                        string RecordTitle = (string)dtSyncDeleteRecord.Rows[iIndex]["RecordTitle"];
                        string DeletedBy = (string)dtSyncDeleteRecord.Rows[iIndex]["DeletedBy"];
                        
                        RetSyncDeleteRecord +=
                               "  <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                               "      <td>" + (iIndex + 1) + "</td>" +
                               "      <td>" + ObjectName + "</td>" +
                               "      <td>" + DeletedDate.ToString("dd/MM/yyyy") + "</td>" +
                               "      <td>" + RecordTitle + "</td>" +
                               "      <td>" + DeletedBy + "</td>" +
                               " </tr>";
                    }
                    RetSyncDeleteRecord += "</table>";
                }
                return RetSyncDeleteRecord;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

    }
}
